package com.example.rbacmanage.controller.app;

import com.example.rbacmanage.annotation.CurrentUser;
import com.example.rbacmanage.common.Result;
import com.example.rbacmanage.common.enums.OrderStatusEnum;
import com.example.rbacmanage.entity.app.AppMenu;
import com.example.rbacmanage.entity.app.MenuOrder;
import com.example.rbacmanage.entity.app.MinAppConfig;
import com.example.rbacmanage.property.DefinedProperty;
import com.example.rbacmanage.service.app.AppMenuService;
import com.example.rbacmanage.service.app.MenuOrderService;
import com.example.rbacmanage.service.app.MinAppConfigService;
import com.example.rbacmanage.util.WxUtil;
import com.wechat.pay.java.service.payments.jsapi.model.Payer;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.model.TransactionPayer;
import com.wechat.pay.java.service.refund.model.Refund;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/app/menuorder")
@Slf4j
public class MenuOrderAppController {
    @Autowired
    private MenuOrderService menuOrderService;
    @Autowired
    private MinAppConfigService minAppConfigService;
    @Autowired
    private AppMenuService appMenuService;
    @Autowired
    private DefinedProperty definedProperty;

    @GetMapping("/page")
    public Result page(MenuOrder menuOrder){
        return Result.success(menuOrderService.page(menuOrder));
    }

    @GetMapping("/count")
    public Result count(MenuOrder menuOrder){
        return Result.success(menuOrderService.getCount(menuOrder));
    }

    @PostMapping("/add")
    public Result add(@RequestBody MenuOrder menuOrder){
        menuOrderService.save(menuOrder);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody MenuOrder menuOrder){
        menuOrderService.update(menuOrder);
        return Result.success();
    }


    /**
     * 支付回调
     * @param menuOrderIds
     * @return
     */
    @PostMapping("/pay_notify_url/{menuOrderIds}/{outTradeNo}")
    public Result pay_notify_url(@PathVariable("menuOrderIds") String menuOrderIds,@PathVariable("outTradeNo")String outTradeNo){
        List<Long> menuOrderIdsList = Arrays.stream(menuOrderIds.split("&")).map(Long::valueOf).toList();
        for (Long menuOrderId : menuOrderIdsList) {
            MenuOrder menuOrder = menuOrderService.getById(menuOrderId);
            if (menuOrder != null) {
                menuOrder.setOrderStatus(OrderStatusEnum.PAID.getType());
                menuOrder.setOutTradeNo(outTradeNo);
                AppMenu menu = appMenuService.getById(menuOrder.getMenuId());
                if (menu != null && menu.getShopId() != null) {
                    MinAppConfig minAppConfig = minAppConfigService.getOneByShopIdAndType(menu.getShopId(), "0");
                    if (minAppConfig != null) {
                        minAppConfig.setPrivateKeyPath(minAppConfig.getPrivateKeyPath().replace("/profile/",definedProperty.getUploadProfile()));
                        minAppConfig.setPublicKeyPath(minAppConfig.getPublicKeyPath().replace("/profile/",definedProperty.getUploadProfile()));
                        Transaction transaction = WxUtil.queryOrderByOutTradeNo(minAppConfig.getMerchantId(),
                                minAppConfig.getPrivateKeyPath(),minAppConfig.getPublicKeyPath(),minAppConfig.getMerchantSerialNumber()
                                ,minAppConfig.getApiV3Key(),outTradeNo,minAppConfig.getPublicKeyId());
                        if (transaction != null) {
                            menuOrder.setTransactionId(transaction.getOutTradeNo());
                            menuOrder.setTradeState(transaction.getTradeState().name());
                            menuOrder.setTradeStateDesc(transaction.getTradeStateDesc());
                            menuOrder.setTradeType(transaction.getTradeType().name());
                            menuOrder.setSuccessTime(transaction.getSuccessTime());
                            TransactionPayer payer = transaction.getPayer();
                            if (payer != null) {
                                menuOrder.setOpenId(payer.getOpenid());
                            }
                        }
                    }
                }
                menuOrderService.update(menuOrder);
            }
        }
        return Result.success();
    }
}
